// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Cannon Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.originPos.x
private alias object.value2 : object.originPos.y
private alias object.value3 : object.startPos.y
private alias object.value4 : object.cannonHeight
private alias object.value5 : object.shotCount
private alias object.value6 : object.lastAttackHeight

// States
private alias 0 : OOZEGGMANCANNON_SETUP
private alias 1 : OOZEGGMANCANNON_RISE
private alias 2 : OOZEGGMANCANNON_PREPARE
private alias 3 : OOZEGGMANCANNON_ATTACK
private alias 4 : OOZEGGMANCANNON_LOWER

// Player Aliases
private alias object.xpos : player.xpos


// ========================
// Tables
// ========================

private table OOZEggmanCannon_cannonHeights
	-0x780000, -0x800000, -0x700000, -0x510000
end table


// ========================
// Events
// ========================

event ObjectUpdate
	object.angle += 4
	object.angle &= 0x1FF
	if player[0].xpos < object.xpos
		object.direction = FLIP_NONE
	else
		object.direction = FLIP_X
	end if

	switch object.state
	case OOZEGGMANCANNON_SETUP
		object.originPos.x = object.xpos
		object.originPos.y = object.ypos
		object.cannonHeight = object.ypos
		object.cannonHeight -= 0x700000
		object.startPos.y = object.ypos
		object.yvel = -0x8000
		object.state = OOZEGGMANCANNON_RISE
		break

	case OOZEGGMANCANNON_RISE
		object.originPos.y += object.yvel
		if object.originPos.y <= object.cannonHeight
			object.originPos.y = object.cannonHeight
			object.shotCount = 3
			object.timer = 128
			object.lastAttackHeight = 0
			object.state++
		end if
		break

	case OOZEGGMANCANNON_PREPARE
		if object.animationTimer > 0
			object.animationTimer--
		else
			object.frame = 0
		end if

		object.timer--
		if object.timer == 0
			if object.shotCount > 0
				Rand(temp0, 4)
				while temp0 == object.lastAttackHeight
					if stage.milliSeconds > 50
						temp0++
					else
						temp0--
					end if
					temp0 &= 3
				loop
				object.lastAttackHeight = temp0

				GetTableValue(object.cannonHeight, temp0, OOZEggmanCannon_cannonHeights)
				object.cannonHeight += object.startPos.y
				if object.cannonHeight < object.originPos.y
					object.yvel = -0x8000
				else
					object.yvel = 0x8000
				end if
				object.state = OOZEGGMANCANNON_ATTACK
			else
				object.yvel = 0x8000
				object.state = OOZEGGMANCANNON_LOWER
			end if
		end if
		break

	case OOZEGGMANCANNON_ATTACK
		object.originPos.y += object.yvel
		checkResult = false
		if object.yvel < 0
			if object.originPos.y <= object.cannonHeight
				object.originPos.y = object.cannonHeight
				checkResult = true
			end if
		else
			if object.originPos.y >= object.cannonHeight
				object.originPos.y = object.cannonHeight
				checkResult = true
			end if
		end if

		if checkResult == true
			object.animationTimer = 7
			object.frame = 1
			object.shotCount--
			object.timer = 40
			object.state = OOZEGGMANCANNON_PREPARE

			Cos(object.xpos, object.angle)
			object.xpos <<= 11
			object.xpos += object.originPos.x

			Sin(object.ypos, object.angle)
			object.ypos <<= 9
			object.ypos += object.originPos.y

			CreateTempObject(TypeName[Eggman Laser], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 2
			object[tempObjectPos].direction = object.direction
			if object.direction == FLIP_NONE
				object[tempObjectPos].xvel = -0x40000
			else
				object[tempObjectPos].xvel = 0x40000
			end if
			PlaySfx(SfxName[Small Laser], false)
		end if
		break

	case OOZEGGMANCANNON_LOWER
		object.originPos.y += object.yvel
		if object.originPos.y > object.startPos.y
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch
end event


event ObjectDraw
	temp1 = object.angle
	temp1 -= 288
	temp1 &= 0x1FF
	temp0 = 0
	temp2 = object.originPos.y
	temp2 += 0x780000
	while temp0 < 8
		Cos(object.xpos, temp1)
		object.xpos <<= 11
		object.xpos += object.originPos.x

		Sin(object.ypos, temp1)
		object.ypos <<= 9
		object.ypos += temp2

		DrawSprite(2)

		temp0++
		temp1 += 32
		temp1 &= 0x1FF
		temp2 -= 0xF0000
	loop

	Cos(object.xpos, temp1)
	object.xpos <<= 11
	object.xpos += object.originPos.x

	Sin(object.ypos, temp1)
	object.ypos <<= 9
	object.ypos += temp2

	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)

	// Why is there collision in the draw event?????
	BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
	if checkResult == true
		CallFunction(Player_Hit)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-32, -8, 40, 16, 141, 207)
		SpriteFrame(-32, -8, 40, 16, 141, 224)
		SpriteFrame(-8, -8, 16, 16, 379, 196)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-32, -8, 40, 16, 512, 180)
		SpriteFrame(-32, -8, 40, 16, 553, 180)
		SpriteFrame(-8, -8, 16, 16, 355, 214)
	end if
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-32, -8, 40, 16, 141, 224)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-32, -8, 40, 16, 553, 180)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
